package com.gxa.retail_admin.controller;

import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.R;
import com.gxa.modules.pojo.R_User;
import com.gxa.modules.service.ManageService;
import io.swagger.annotations.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author: wangyifan
 * @Date: 2021/12/27 18:58
 * @Description: $end$
 * @Version: V1.0
 */
@RestController
@Api(value = "管理模块",tags = "管理模块(王议帆)")
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    private ManageService manageService;

    @PostMapping("/queryRole")
    @ApiOperation(value = "查询角色接口")
    @RequiresPermissions("角色管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页码数",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "string",value = "字符串",paramType = "query",dataType = "String",required = false)
    })
    /**角色查询*/
    public JsonResult queryRole(@ApiParam(name = "currentPage",defaultValue = "1")Integer currentPage,@RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize,@RequestParam(name = "string",defaultValue = " ")String string){
        JsonResult jsonResult = manageService.queryRole(currentPage,pageSize,string);
        return jsonResult;
    }


    @PostMapping("/addRole")
    @ApiOperation(value = "增加角色接口")
    @RequiresPermissions("角色管理")
    @ApiImplicitParams({
           @ApiImplicitParam(name = "roleName",value = "角色名",paramType = "query",dataType = "String",required = true),
           @ApiImplicitParam(name = "powers",value = "权限集合",paramType = "query",dataType = "List",required = true)
    })
    /**创建角色*/
    public R addRole(@RequestParam(name = "roleName") String roleName,@RequestParam(name = "powers")List<Integer> powers){
        R r = manageService.addRole(roleName,powers);
        return r;
    }

    @PostMapping("/updateRole")
    @ApiOperation(value = "编辑角色接口")
    @RequiresPermissions("角色管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName",value = "角色名",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "powers",value = "权限集合",paramType = "query",dataType = "List",required = true),
            @ApiImplicitParam(name = "roleId",value = "角色id",paramType = "query",dataType = "int",required = true)
    })
    /**编辑角色*/
    public R updateRole(@RequestParam(name = "roleId")  Integer roleId,@RequestParam(name = "roleName") @NotNull String roleName,@RequestParam(name = "powers")List<Integer> powers){
        R r = manageService.updateRole(roleId,roleName,powers);
        return r;
    }

    @PostMapping(value = "/deleteRole")
    @ApiOperation(value = "删除角色接口")
    @RequiresPermissions("角色管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId",value = "角色id",paramType = "query",dataType = "int",required = true)
    })
    /**删除角色*/
    public R deleteRole(@RequestParam(name = "roleId") Integer roleId){
        R r = manageService.deleteRole(roleId);
        return r;
    }

    @PostMapping("/addUser")
    @ApiOperation(value = "创建账户接口")
    @RequiresPermissions("账号管理")
    public R addUser(@RequestBody @ApiParam(name = "user",value = "用户实体")R_User user){
        R r = manageService.addUser(user);

        return r;
    }

        @PostMapping("/queryUser")
        @ApiOperation(value = "查询账号接口")
        @RequiresPermissions("账号管理")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "currentPage",value = "当前页码数",paramType = "query",dataType = "int",required = false),
                @ApiImplicitParam(name = "pageSize",value = "每页条数",paramType = "query",dataType = "int",required = false),
                @ApiImplicitParam(name = "string",value = "字符串",paramType = "query",dataType = "String",required = false)
        })
        /**账号查询*/
        public JsonResult queryUser(@RequestParam(name = "currentPage",defaultValue = "1")Integer currentPage,@RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize,@RequestParam(name = "string",defaultValue = " ")String string){
            System.out.println("1111");
            JsonResult jsonResult = manageService.queryUser(currentPage,pageSize,string);

            return jsonResult;
    }

    @PostMapping("/updateUser")
    @ApiOperation(value = "编辑账户接口")
    @RequiresPermissions("账号管理")
    /**编辑账户*/
    public R updateUser(@RequestBody @ApiParam(name = "user",value = "用户实体")R_User user){
        R r = manageService.updateUser(user);

        return r;
    }

    @PostMapping(value = "/deleteUser")
    @ApiOperation(value = "删除账户接口")
    @RequiresPermissions("账号管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "账户id",paramType = "query",dataType = "long",required = true)
    })
    /**删除角色*/
    public R deleteUser(@RequestParam(name = "userId")Long userId){
        R r = manageService.deleteUser(userId);
        return r;
    }




}
